using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BANSACH.BusinessLayer.DataLayer;
using BANSACH.BusinessLayer;
namespace BANSACH_GUI
{
    public partial class frmTblNXBDm : FormBase
    {
        DataView data;
        TblNXBFactory objDao;
        TblNXB _entity;

        public TblNXB Entity
        {
            get { return _entity; }
            set { _entity = value; }
        }
        public frmTblNXBDm()
        {
            InitializeComponent();
        }

        private void frmTblNXBDm_Load(object sender, EventArgs e)
        {
            // Tạo các cột và đặt thuộc tính cho Grid
            ConfigGrid();
            // Load danh sách nhà xuất bản
            objDao = new TblNXBFactory();
            data = objDao.GetAll().DefaultView;
            grvData.DataSource = data;
        }
        private void ConfigGrid()
        {
            grvData.ModeColorRow = 1;
            grvData.SetColorRow(System.Drawing.Color.AliceBlue);            
			grvData.AddColumns("TextBox","MaNXB","Mã",100,true,true,"ML","");
			grvData.AddColumns("TextBox","TenNXB","Tên",200,true,true,"ML","");
			grvData.AddColumns("TextBox","DiaChi","Địa chỉ",200,true,true,"ML","");
			grvData.AddColumns("TextBox","Email","Email",100,true,true,"ML","");
			grvData.AddColumns("TextBox","DienThoai","Điện thoại",100,true,true,"ML","");
        }
        private void SetDataFilter()
        {
            try
            {
                string filter = "";
                if (txt_Find.Text.Trim() != "")
                    filter = string.Format("TenNXB LIKE '%{0}%' OR DienThoai LIKE '%{0}%'", txt_Find.Text);
                data.RowFilter = filter;
            }
            catch
            { }
        }

        private void grvData_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
                ActiveControl = txt_Find;
        }

        private void grvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Apply();
        }

        private void grvData_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
                Apply();
            if (e.Control)
                ActiveControl = grvData;
        }
        private void Apply()
        {
            _entity = new TblNXB();
            if (grvData.SelectedCells.Count == 0) return;
            int idrow = grvData.SelectedCells[0].RowIndex;
            _entity.MaNXB = grvData["MaNXB", idrow].Value.ToString();
            _entity.TenNXB = grvData["TenNXB", idrow].Value.ToString();
            _entity.DiaChi = grvData["DiaChi", idrow].Value.ToString();
            _entity.Email = grvData["Email", idrow].Value.ToString();
            _entity.DienThoai = grvData["DienThoai", idrow].Value.ToString();
            this.DialogResult = DialogResult.OK;
        }

        private void txt_Find_TextChanged(object sender, EventArgs e)
        {
            string filter = "";
            if (txt_Find.Text.Trim() != "")
                filter = string.Format("TenNXB LIKE '%{0}%' OR DienThoai LIKE '%{0}%'", txt_Find.Text);
            data.RowFilter = filter;
        }
    }
}
